package com.ketayao.utils;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 14-7-10
 * Time: 下午2:06
 * To change this template use File | Settings | File Templates.
 */
public class ConfUtil {
    public static Logger logger = Logger.getLogger(ConfUtil.class);
     public static Properties props = new Properties();
    private static ApplicationContext applicationContextDao = null;
    private static ApplicationContext applicationContext = null;

    /**
     * 获得加载properties文件
     *
     * @param url
     * @return
     */
    public static Properties getConf(String url) {
        try {
            // 由于getResourceAsStream方法对所读取的文件位置有要求，所以，要用FileReader方法
//            InputStream in = ConfUtil.class.getResourceAsStream(url);
            FileReader in = new FileReader(url);
            props.load(in);
            return props;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return props;
    }

    private ConfUtil() {
    }
//
//    static {
//        String configPathName = "classpath:/config.properties";
//        getConf(configPathName);
//    }
    
    
    public  static String getPropsValue(String key) {     	
    	  
    	        try {
    	        	InputStream inputStream = ConfUtil.class.getClassLoader().getResourceAsStream("config.properties");  
					props.load(inputStream);
				} catch (IOException e) {				 
					 logger.error(e.getMessage());
				}  
    	        return props.getProperty(key);  
    	} 

    /**
     * 获得spring配置的数据库连接
     *
     * @return
     */
    public static ApplicationContext getSingletonApplicationContextDao() {
        String springFilePath = props.getProperty("springDaoFilePath");
        if (applicationContextDao == null)
            applicationContextDao = new FileSystemXmlApplicationContext(springFilePath);
        return applicationContextDao;
    }

    /**
     * 获得 spring 配置的上下文实体
     *
     * @return
     */
    public static ApplicationContext getSingletonApplicationContext() {
        String springFilePath = props.getProperty("springFilePath");
        if (applicationContext == null)
            applicationContext = new FileSystemXmlApplicationContext(springFilePath);
        return applicationContext;
    }

    // 通过配置航空公司二字节码判断是不是要处理的报文
    public static boolean IsCanCompony(String  code,String  componys[]) {
        boolean iscan=false;
        for (int i = 0; i < componys.length; i++) {
            if (code.trim().toUpperCase().equals(componys[i].toUpperCase())) {
                iscan=true;
            }
        }

        return  iscan;
    }

    public static void main(String[] args) throws IOException {
//        String subject = getConf("config/regexValidCheck.properties").getProperty("regexAcarsFreeText");
//        System.out.println(subject);
        FileReader file = new FileReader("config/regexValidCheck.properties");
        props.load(file);
        String subject = props.getProperty("regexAcarsFreeText");
//        System.out.println(subject);

//        System.out.println("HC00001".substring(0, 2));
    }

}
